package depta.DOMTree;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import org.lobobrowser.html.domimpl.HTMLDocumentImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.w3c.dom.Document;
import org.w3c.dom.html2.HTMLDocument;
import org.w3c.tidy.Tidy;
import org.xml.sax.SAXException;

public class ParserRunnable implements Runnable {

	private static final int REMOTE = 1;
	private static final int LOCAL = 0;
	private String urlString;
	private DocumentBuilder builder;
	private Map<Integer, HTMLDocumentImpl> map;
	private Integer index;
	private File localFile;
	private Map<File, HTMLDocument> localMap;
	private int mode;

	public ParserRunnable(Integer index,String urlString, DocumentBuilder builder,Map<Integer, HTMLDocumentImpl> map){
		this.urlString = urlString;
		this.builder = builder;
		this.map = map;
		this.index = index;
		this.mode = REMOTE;
	}
	
	public ParserRunnable(File file, DocumentBuilder builder, Map<File, HTMLDocument> map) {
		this(null,null,builder,null);
		this.localFile = file;
		this.localMap = map;
		this.mode = LOCAL;
	}

	@Override
	public void run() {
		       
        try {
       
			if (mode == REMOTE){
        	
		    	URL url = new URL(urlString);
		         
		    	URLConnection conn = url.openConnection();
		    	
		    	conn.setReadTimeout(10000);
		    	
		    	conn.setConnectTimeout(10000);
		    	
		        InputStream in = conn.getInputStream();
	                     	
	        	Reader reader = new InputStreamReader(in/*, "ISO-8859-1"*/);
	            
	        	InputStream input = createCleanInput(reader);
        	
	        	Document d = builder.parse(input);
	            
	        	HTMLDocumentImpl document = (HTMLDocumentImpl) d;
	        	
	        	map.put(index, document);
	        	
	        	in.close();
	        	
        	}else if (mode == LOCAL){
        		
        		InputStream input = createCleanInput(new FileReader(localFile));
        		
	        	Document d = builder.parse(input);
	            
	        	HTMLDocumentImpl document = (HTMLDocumentImpl) d;

        		localMap.put(localFile, document);

        	}


        } catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

        }

	}

	private InputStream createCleanInput(Reader reader) throws IOException {
		
		StringWriter sw = new StringWriter();
		
		Tidy tidy = new Tidy();
		
		tidy.setForceOutput(true);
		
		tidy.parseDOM(reader, sw);
		
		return new ByteArrayInputStream(sw.toString().getBytes());
		
	}

}
